﻿#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)

/*写一个程序，生成前n个素数，并在main程序中打印这n个素数(要求用vim编写)。
// generatePrimes.c
// 编译：gcc generatePrimes.c -o generatePrimes
// 执行：./generatePrimes
long long* generatePrimes(int n);​
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

bool isPrime(long long n) {
    if (n == 0 || n == 1) return false;

    for (long long int i = 2; i * i <= n; i++) {
        if (n % i == 0)
            return false;
    }
    return true;
}

long long* generatePrimes(int n) {
    long long* prime = (long long*)malloc(n*sizeof(long long));
    if (!prime)  return NULL;
    int count = 0;
    long long num = 2;
    while (count < n) {
       if (isPrime(num)) {
            prime[count] = num;
            count++;       
        }
         num++;
    }
     return prime;
}

int main()
{
     int n = 10;
     long long* primes = generatePrimes(n);
     for (int i = 0; i < n; i++) {
          printf("%lld ", primes[i]);
        
     }
      printf("\n");
      free(primes);      
      
      return 0;
 }

